import 'package:enjoy_plus_hm/pages/404.dart';
import 'package:enjoy_plus_hm/pages/home/detail.dart';
import 'package:enjoy_plus_hm/pages/house/building_list.dart';
import 'package:enjoy_plus_hm/pages/house/house_detail.dart';
import 'package:enjoy_plus_hm/pages/house/house_form.dart';
import 'package:enjoy_plus_hm/pages/house/house_list.dart';
import 'package:enjoy_plus_hm/pages/house/location_list.dart';
import 'package:enjoy_plus_hm/pages/house/room_list.dart';
import 'package:enjoy_plus_hm/pages/login/index.dart';
import 'package:enjoy_plus_hm/pages/profile/index.dart';
import 'package:enjoy_plus_hm/pages/tab_bar/index.dart';
import 'package:enjoy_plus_hm/stores/counter.dart';
import 'package:enjoy_plus_hm/utils/toast.dart';
import 'package:enjoy_plus_hm/utils/token.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => CounterModel()),
      ], //! 必须是箭头函数
      child: MaterialApp(
        routes: {
          '/': (context) => TabBarPage(),
          '/detail': (context) => const NoticeDetailPage(),
          '/login': (context) => const LoginPage(),
        },
        initialRoute: '/',
        onGenerateRoute: (settings) {
          final token = TokenManager().getToken() ?? '';
          if (token.isEmpty && settings.name != '/login') {
            ToastUtil.showError('请先登录');
            return MaterialPageRoute(builder: (context) => LoginPage());
          }
          if (token.isNotEmpty && settings.name == '/profile') {
            return MaterialPageRoute(
              builder:
                  (context) => ProfilePage(userInfo: settings.arguments as Map),
            );
          }
          if (token.isNotEmpty && settings.name == '/house') {
            return MaterialPageRoute(builder: (context) => HouseList());
          }
          if (token.isNotEmpty && settings.name == '/location') {
            return MaterialPageRoute(builder: (context) => LocationList());
          }
          if (token.isNotEmpty && settings.name == '/building') {
            return MaterialPageRoute(
              builder:
                  (context) =>
                      BuildingList(point: (settings.arguments as Map)['point']),
            );
          }
          if (token.isNotEmpty && settings.name == '/room') {
            Map obj = settings.arguments as Map;
            return MaterialPageRoute(
              builder:
                  (context) =>
                      RoomList(point: obj['point'], building: obj['building']),
            );
          }
          if (token.isNotEmpty && settings.name == '/house_from') {
            return MaterialPageRoute(
              builder:
                  (context) => HouseForm(houseInfo: settings.arguments as Map),
            );
          }
          if (token.isNotEmpty && settings.name == '/house_detail') {
            return MaterialPageRoute(
              builder:
                  (context) =>
                      HouseDetail(id: (settings.arguments as Map)['id']),
            );
          }
          return MaterialPageRoute(builder: (context) => const NotFound());
        },
      ),
    ),
  );
}
